﻿// Decompiled with JetBrains decompiler
// Type: Microsoft.InfoCards.PolicyFactory
// Assembly: infocard, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: 8E14765A-6610-409A-BA36-099A0642905D
// Assembly location: E:\git\ALLIDA\windll\infocard.exe

using Microsoft.InfoCards.Diagnostics;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IdentityModel.Selectors;
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Security.Tokens;
using System.Text;
using System.Xml;

namespace Microsoft.InfoCards
{
  internal class PolicyFactory
  {
    private PolicyFactory()
    {
    }

    public static Policy CreatePolicyFromUnwrappedPolicyXml(string originalPolicyXml)
    {
      string rstPolicyXml = string.Format((IFormatProvider) CultureInfo.InvariantCulture, "<{0}:{1} xmlns:{0}='{2}'>{3}</{0}:{1}>", (object) XmlNames.WSSecurityPolicyXmlSoap2005.Instance.DefaultPrefix, (object) XmlNames.WSSecurityPolicyXmlSoap2005.Instance.RequestSecurityTokenTemplate, (object) XmlNames.WSSecurityPolicyXmlSoap2005.Instance.Namespace, (object) originalPolicyXml);
      return new Policy(originalPolicyXml, rstPolicyXml);
    }

    public static InfoCardPolicy CreatePolicyForCustomTokenProvider(
      EndpointAddress immediateTokenRecipient,
      IssuedSecurityTokenParameters issuedTokenParameters,
      ProtocolProfile profile)
    {
      ParsedPolicy parsedPolicy = PolicyFactory.CreateParsedPolicy((IEnumerable<XmlElement>) issuedTokenParameters.CreateRequestParameters(profile.MsgSecurityVersion, (SecurityTokenSerializer) profile.TokenSerializer));
      return new InfoCardPolicy(immediateTokenRecipient, issuedTokenParameters.IssuerAddress, parsedPolicy, PolicyUsageContext.GetToken, (string) null, 0U, (RecipientIdentity) null, false);
    }

    public static InfoCardPolicy CreatePolicyForGetBrowserTokenRequest(
      CardSpacePolicyElement policyElement,
      Uri recipientPolicyNoticeLink,
      uint recipientPolicyNoticeVersion,
      RecipientIdentity recipientIdentity,
      PolicyUsageContext usageContext)
    {
      EndpointAddress immediateTokenRecipient = (EndpointAddress) null;
      if (!string.IsNullOrEmpty(policyElement.Target.OuterXml))
        immediateTokenRecipient = PolicyFactory.CreateEndpointAddressFromString(policyElement.Target.OuterXml);
      EndpointAddress issuer = (EndpointAddress) null;
      if (policyElement.Issuer != null && !string.IsNullOrEmpty(policyElement.Issuer.OuterXml))
        issuer = PolicyFactory.CreateEndpointAddressFromString(policyElement.Issuer.OuterXml);
      ParsedPolicy parsedPolicy = PolicyFactory.CreateParsedPolicy((IEnumerable<XmlElement>) policyElement.Parameters);
      return new InfoCardPolicy(immediateTokenRecipient, issuer, parsedPolicy, usageContext, (Uri) null != recipientPolicyNoticeLink ? recipientPolicyNoticeLink.ToString() : (string) null, recipientPolicyNoticeVersion, recipientIdentity, false);
    }

    public static InfoCardPolicy CreatePolicyForIntermediateGetBrowserTokenRequest(
      CardSpacePolicyElement policyElement,
      Uri recipientPolicyNoticeLink,
      uint recipientPolicyNoticeVersion,
      RecipientIdentity recipientIdentity)
    {
      EndpointAddress immediateTokenRecipient = (EndpointAddress) null;
      if (!string.IsNullOrEmpty(policyElement.Target.OuterXml))
        immediateTokenRecipient = PolicyFactory.CreateEndpointAddressFromString(policyElement.Target.OuterXml);
      EndpointAddress issuer = (EndpointAddress) null;
      if (policyElement.Issuer != null && !string.IsNullOrEmpty(policyElement.Issuer.OuterXml))
        issuer = PolicyFactory.CreateEndpointAddressFromString(policyElement.Issuer.OuterXml);
      ParsedPolicy parsedPolicy = PolicyFactory.CreateParsedPolicy((IEnumerable<XmlElement>) policyElement.Parameters);
      return new InfoCardPolicy(immediateTokenRecipient, issuer, parsedPolicy, PolicyUsageContext.Intermediate, (Uri) null != recipientPolicyNoticeLink ? recipientPolicyNoticeLink.ToString() : (string) null, recipientPolicyNoticeVersion, recipientIdentity, false);
    }

    public static InfoCardPolicy CreatePolicyForGetTokenRequest(
      BinaryReader reader,
      string recipientXml,
      string issuerXml,
      string policyXml,
      bool isManaged)
    {
      if (!string.IsNullOrEmpty(policyXml))
      {
        string xml = string.Format((IFormatProvider) CultureInfo.InvariantCulture, "<{0}:{1} xmlns:{0}='{2}'>{3}</{0}:{1}>", (object) XmlNames.WSSecurityPolicyXmlSoap2005.Instance.DefaultPrefix, (object) XmlNames.WSSecurityPolicyXmlSoap2005.Instance.RequestSecurityTokenTemplate, (object) XmlNames.WSSecurityPolicyXmlSoap2005.Instance.Namespace, (object) policyXml);
        XmlDocument xmlDocument = new XmlDocument();
        xmlDocument.LoadXml(xml);
        Collection<XmlElement> collection = new Collection<XmlElement>();
        foreach (XmlElement childNode in xmlDocument.DocumentElement.ChildNodes)
          collection.Add(childNode);
        ParsedPolicy parsedPolicy = PolicyFactory.CreateParsedPolicy((IEnumerable<XmlElement>) collection);
        return new InfoCardPolicy(!string.IsNullOrEmpty(recipientXml) ? PolicyFactory.CreateEndpointAddressFromString(recipientXml) : (EndpointAddress) null, !string.IsNullOrEmpty(issuerXml) ? PolicyFactory.CreateEndpointAddressFromString(issuerXml) : (EndpointAddress) null, parsedPolicy, PolicyUsageContext.GetToken, (string) null, 0U, (RecipientIdentity) null, isManaged);
      }
      if (!isManaged)
        throw InfoCardTrace.ThrowHelperError((Exception) new PolicyValidationException(SR.GetString("InvalidPolicySpecified")));
      return new InfoCardPolicy(!string.IsNullOrEmpty(recipientXml) ? PolicyFactory.CreateEndpointAddressFromString(recipientXml) : (EndpointAddress) null, !string.IsNullOrEmpty(issuerXml) ? PolicyFactory.CreateEndpointAddressFromString(issuerXml) : (EndpointAddress) null, (ParsedPolicy) null, PolicyUsageContext.GetToken, (string) null, 0U, (RecipientIdentity) null, isManaged);
    }

    private static ParsedPolicy CreateParsedPolicy(IEnumerable<XmlElement> elements)
    {
      try
      {
        XmlElement xmlElement = (XmlElement) null;
        StringBuilder stringBuilder1 = new StringBuilder();
        StringBuilder stringBuilder2 = new StringBuilder();
        foreach (XmlElement element in elements)
        {
          stringBuilder2.Append(element.OuterXml);
          if (element.LocalName == "SecondaryParameters" && element.NamespaceURI == "http://docs.oasis-open.org/ws-sx/ws-trust/200512")
            xmlElement = element;
          else
            stringBuilder1.Append(element.OuterXml);
        }
        if (xmlElement != null)
          return new ParsedPolicy(stringBuilder2.ToString(), stringBuilder1.ToString(), xmlElement.InnerXml);
        return new ParsedPolicy(stringBuilder2.ToString(), stringBuilder1.ToString(), (string) null);
      }
      catch (XmlException ex)
      {
        throw InfoCardTrace.ThrowHelperError((Exception) new PolicyValidationException(SR.GetString("InvalidPolicySpecified"), (Exception) ex));
      }
    }

    private static EndpointAddress CreateEndpointAddressFromString(string fragment)
    {
      try
      {
        using (XmlReader reader = PolicyFactory.LoadXmlIntoReader(fragment.Trim()))
        {
          reader.MoveToElement();
          return EndpointAddress.ReadFrom(XmlDictionaryReader.CreateDictionaryReader(reader));
        }
      }
      catch (Exception ex)
      {
        if (!InfoCardTrace.IsFatal(ex))
          throw InfoCardTrace.ThrowHelperError((Exception) new PolicyValidationException(SR.GetString("ServiceInvalidEprInPolicy"), ex));
        throw;
      }
    }

    private static XmlReader LoadXmlIntoReader(string fragment)
    {
      return InfoCardSchemas.CreateReader(fragment);
    }
  }
}
